package com.tistory.iam1492.util;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.util.Log;

public class ImageUtil {

	public static byte[] BitmapToByteArray (Bitmap bitmap) {
		ByteArrayOutputStream  byteArray = new ByteArrayOutputStream();
		bitmap.compress(CompressFormat.PNG, 100, byteArray);
		return byteArray.toByteArray();
	}

	public static Bitmap DrawBarcode(byte[] data) { 
		return BitmapFactory.decodeByteArray(data, 0, data.length);   
	}
	
	static public Bitmap getImageBitmap(String imgUrl){
    	URL url = null;
    	Bitmap bm = null;
    	try {
    		Log.e("ImageManager", "imgUrl=" + imgUrl);
    		if (imgUrl == null || imgUrl.length()==0)
    			return null;
			url = new URL(imgUrl);
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			conn.connect();
			InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();
            conn.disconnect();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bm;   	
    }
}
